<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\DB;

return new class extends Migration
{
    /*
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (! Schema::hasTable('consult_setting')) {
            Schema::create('consult_setting', function (Blueprint $table) {
                $table->id();
                $table->json('user_avatar');
                $table->string('youdao_id')->nullable();
                $table->string('youdao_key')->nullable();
                $table->tinyInteger('service')->default(1);
                $table->json('your_first_reply')->nullable();
                $table->json('offline_prompt');
                $table->string('facebook')->nullable();
                $table->string('messenger')->nullable();
                $table->string('instagram')->nullable();
                $table->string('whatsapp')->nullable();
                $table->string('line')->nullable();
                $table->string('snapchat')->nullable();
                $table->string('wecom')->nullable();
                $table->string('tiktok')->nullable();
                $table->string('kwai')->nullable();
                $table->string('vkontakte')->nullable();
                $table->string('telegram')->nullable();
            });
            DB::table('consult_setting')->insert([
                'user_avatar' => json_encode([['url'=>'/plugin/consult/image/user.png'],['url'=>'/plugin/consult/image/user1.png'],['url'=>'/plugin/consult/image/user2.png'],['url'=>'/plugin/consult/image/user3.png']]),
                'youdao_id' => '',
                'youdao_key' => '',
                'your_first_reply'=>json_encode([['name'=>'中文','code'=>'zh_cn','value'=>'欢迎光临，客服正在赶来，请稍后...'],['name'=>'English','code'=>'en','value'=>'Welcome, customer service is on their way, please wait a moment']]),
                'offline_prompt' => json_encode([['name'=>'中文','code'=>'zh_cn','value'=>'您好!客服正在休息中，请留下您的联系方式，客服上线后会第一时间联系您~'],['name'=>'English','code'=>'en','value'=>'Hello! The customer service representative is currently on break. Please leave your contact information and they will get in touch with you as soon as they are available.']]),
            ]);
        }else{
            Schema::table('consult_setting', function (Blueprint $table) {
                if (!Schema::hasColumn('consult_setting', 'your_first_reply')) {
                    $table->string('your_first_reply')->nullable();
                    DB::table('consult_setting')->where('id',1)->update(['your_first_reply'=>json_encode([['name'=>'中文','code'=>'zh_cn','value'=>'欢迎光临，客服正在赶来，请稍后...'],['name'=>'English','code'=>'en','value'=>'Welcome, customer service is on their way, please wait a moment']])]);
                }
                if (!Schema::hasColumn('consult_setting', 'tiktok')) {
                    $table->string('tiktok')->nullable();
                }
                if (!Schema::hasColumn('consult_setting', 'kwai')) {
                    $table->string('kwai')->nullable();
                }
                if (!Schema::hasColumn('consult_setting', 'vkontakte')) {
                    $table->string('vkontakte')->nullable();
                }
                if (!Schema::hasColumn('consult_setting', 'telegram')) {
                    $table->string('telegram')->nullable();
                }
            });
        }
        if (! Schema::hasTable('consult_service')) {
            Schema::create('consult_service', function (Blueprint $table) {
                $table->id();
                $table->bigInteger('service_id')->index('service_id');
                $table->string('service_avatar');
                $table->json('service_nickname');
                $table->json('service_describe');
                $table->string('default_lang')->default('zh-CHS');
            });
        }
        if (! Schema::hasTable('consult_verbal')) {
            Schema::create('consult_verbal', function (Blueprint $table) {
                $table->id();
                $table->string('ask');
                $table->text('answer');
                $table->json('picture');
                $table->tinyInteger('status')->default(1);
                $table->timestamps();
            });
        }
        if (! Schema::hasTable('consult_list')) {
            Schema::create('consult_list', function (Blueprint $table) {
                $table->id();
                $table->char('user_id',18)->index('user_id');
                $table->bigInteger('service_id')->index('service_id');
                $table->string('avatar');
                $table->string('nickname',60)->index('nickname');
                $table->string('ip',15);
                $table->string('address',100)->nullable();
                $table->string('remark')->nullable();
                $table->integer('robot')->default(0);
                $table->integer('translate')->default(0);
                $table->string('user_lang');
                $table->string('service_lang')->nullable();
                $table->string('user_language');
                $table->tinyInteger('status')->default(1);
                $table->tinyInteger('state')->default(0);
                $table->timestamps();
            });
        }else{
            Schema::table('consult_list', function (Blueprint $table) {
                if (!Schema::hasColumn('consult_list', 'user_language')) {
                    $table->string('user_language')->nullable()->default('en');
                }
            });
        }
        if (! Schema::hasTable('consult_record')) {
            Schema::create('consult_record', function (Blueprint $table) {
                $table->id();
                $table->char('guid',32)->index('guid');
                $table->bigInteger('consult');
                $table->char('user_id',18)->index('user_id');
                $table->bigInteger('service_id')->index('service_id');
                $table->integer('type');
                $table->string('ip');
                $table->string('nickname');
                $table->string('avatar');
                $table->integer('role');
                $table->string('text');
                $table->string('translate')->nullable();
                $table->json('content');
                $table->tinyInteger('revocation')->default(0);
                $table->datetime('read_time')->nullable();
                $table->datetime('send_time');
                $table->tinyInteger('status')->default(1);
            });
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('consult_setting');
        Schema::dropIfExists('consult_service');
        Schema::dropIfExists('consult_verbal');
        Schema::dropIfExists('consult_list');
        Schema::dropIfExists('consult_record');
    }
};
